<?php


class bizna extends module {

	public $title = "Petites annonces";
	public $author = "hery";
	public $version = "0.9";

	//Methods here:

	public function init()
	{
		$this->timerClass->addTimer("updateini", $this, "updateini", "", 60*10, true);

		$this->bizna_ini = new ini("data/bizna.ini");
	}

	public function destroy()
	{
		$this->updateini();
		$this->timerClass->removeTimer("updateini");
	}
	
	public function updateini()
	{

		$this->bizna_ini->writeIni();

		return true;
	}
	
	
	public function banlist($line, $args)
	{
		
		if ($args['nargs'] == 0)
		{
			$chan = "#serasera";
		}
		else
		{
			$chan = $args['arg1'];
		}

		if (!$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
		
		
		if ($rows =$this->banlist->getVars($chan))
		{
			$this->ircClass->notice($line['fromNick'], "Ireto avy ny banni ao amin'ny " . $chan . ". Soraty hoe " . RED . "!deban " . $chan . " <id>" . NOFORMAT . " raha hanala ny ban ka ny <id> dia ilay isa eo alohany.");
			foreach ($rows as $key => $val)
			{
				$this->ircClass->notice($line['fromNick'], BOLD . $key . " : " . BOLD . $val . RED . " (" . date("d/m/Y H:i:s") . ")"); 
			}
		}
	}
	
	public function deban ($line, $args)
	{
		if ($args['nargs'] == 0)
		{
			$this->ircClass->notice($line['fromNick'], "Tsy ampy ny baiko nampidirinao. Soraty hoe " . RED . "!deban <#canal> <id>" . NOFORMAT . ". Raha tsy fantatrao ny <id> dia soraty hoe " . RED . "!banlist <#canal>");
			return;
		}
		if (substr($args['arg1'], 0, 1) != "#")
		{
			$chan = "#serasera";
			$id = intval($args['arg1']);
		}
		else
		{
			$chan = $args['arg1'];
			$id = intval($args['arg2']);
		}

		if (!$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
	
		
		if ($id == 0)
		{
			$this->ircClass->notice($line['fromNick'], "Diso ny baiko nampidirinao. Soraty hoe " . RED . "!deban <#canal> <id>" . NOFORMAT . ". Ka ny <id> dia ilay laharana ao amin'ny !banlist. Raha tsy fantatrao ny <id> dia soraty hoe " . RED . "!banlist <#canal>");
			return;
		}
		
		$mask = $this->banlist->getIniVal($chan, $id);
		$this->ircChan->changeMode($chan, "-", "b", $mask);
		
	}
	
	public function clearban ()
	{

		if ($chans = $this->banlist->getSections())
		{
			
			foreach ($chans as $chan)
			{
				if($rows = $this->banlist->getVars($chan))
				{
					foreach ($rows as $key => $val)
					{
						if ($key < (time() - 86400) )
						{
							$this->banlist->deleteVar($chan, $key);
						}
					}
				}

			}
		
			$this->banlist->writeIni();
		}

		return true;
	}
	
	public function join($line, $args)
	{

		if ($line['fromNick'] == $this->ircClass->getNick())
		{
			$this->channel_ini->setIniVal("channels", $line['text'], 1);
			$this->channel_ini->writeIni();
		}
	}
	
	public function part($line, $args)
	{
		if ($line['fromNick'] == $this->ircClass->getNick())
		{
			$this->channel_ini->setIniVal("channels", $line['to'], 0);
			$this->channel_ini->writeIni();
		}
	
	}
	
	public function changenick($line, $args)
	{
		
		if (strtolower(substr($line['text'], 0, 7)) == "anonyme" )
		{
		
			$chans = $this->channel_ini->getVars("channels");

			foreach ($chans as $chan => $active)
			{
				if ($active == 1)
				{
					$this->ircClass->sendRaw("kick " . $chan . " " . $line['text'] . " :Soloy ny solonanarana fa efa misy tompony.");
					
					$this->ircClass->privMsg($line['text'], "Efa misy tompony io solonanarana ampidainao io. Soloy hafa dia midira indray");
					$this->ircClass->privMsg($line['text'], "Hanova solonanarana: soraty " . BOLD . "/nick solonanarana vaovao");
					$this->ircClass->privMsg($line['text'], "Hiverina hikarajia: soraty " . BOLD . "/join #serasera");
						
				}
			}
		
		}
	}
	
}

?>
